#include "devlist_item_delegate.h"
#include "devlist_data.h"
#include <QPixmap>

DevlistItemDelegate::DevlistItemDelegate(QObject* parent)
: QStyledItemDelegate(parent)
{
}
	
void DevlistItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const 
{
    QStyledItemDelegate::paint(painter, option, index);

    // 保存当前绘制状态
	painter->save();

    QRect rect = option.rect;  // 获取单元格的矩形区域
    painter->setClipRect(rect); // 设置剪切区域，确保绘制不会超出单元格

    QFont font = painter->font();  // 获取当前字体
//    font.setPointSize(font.pointSize() - 2);  // 缩小字体
//    painter->setFont(font);  // 应用缩小后的字体

    TreeItem* item =  index.data(Qt::UserRole).value<TreeItem*>();
    if (item)
    {
        if (item->DataType() == TREE_DATA_TYPE_GROUP)
        {
            GroupTreeNode* groupNode = (GroupTreeNode*)item;
            int store_count = 0, device_count = 0, online_count = 0;
            groupNode->GetShowCount(store_count, device_count, online_count);

            QString strName = groupNode->GetShowName();
            QFontMetrics fontWidth(font);
            QString strNameEx = fontWidth.elidedText(strName, Qt::ElideRight, rect.width()-100);
            QString strCount;
            if (groupNode->IsStore())
            {
                strCount = QString("%1/%2").arg(online_count).arg(device_count);
            }
            else
            {
                strCount = QString("%1/%2/%3").arg(online_count).arg(device_count).arg(store_count);
            }

            QRect nameRect(rect.left(), rect.top(), rect.width()-100, rect.height());
            QRect countRect(rect.left()+rect.width()-100, rect.top(), 100, rect.height());
            painter->drawText(nameRect, Qt::AlignLeft | Qt::AlignVCenter, strNameEx);
            painter->drawText(countRect, Qt::AlignRight | Qt::AlignCenter, strCount);
        }
        else if (item->DataType() == TREE_DATA_TYPE_DEVICE)
        {
            DeviceTreeNode* deviceNode = (DeviceTreeNode*)item;
            QString strName = deviceNode->GetShowName();
            QFontMetrics fontWidth(font);
            QString strNameEx = fontWidth.elidedText(strName, Qt::ElideRight, rect.width()-rect.height());
            QPixmap flagPixmap;  // 定义一个 QPixmap 对象
            if (deviceNode->IsOnline())
            {
                flagPixmap = QPixmap(":/qss/flatgray/add_bottom.png");
            }
            else
            {
                flagPixmap = QPixmap(":qss/flatgray/add_top.png");
            }

            QRect pixRect(rect.left()-1, rect.top()-1, rect.height()-2, rect.height()-2);
            QRect nameRect(rect.left()+rect.height(), rect.top(), rect.width()-rect.height(), rect.height());
            // 绘制图片
            painter->drawPixmap(pixRect, flagPixmap);
            painter->drawText(nameRect, Qt::AlignLeft | Qt::AlignVCenter, strName);
        }
    }

	// 恢复绘制状态
	painter->restore();
}

QSize DevlistItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    QRect rect = option.rect;
    return QSize(rect.width(), 32);
}

